package com.aurora.source.home.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.aurora.source.R;
import com.aurora.source.base.BaseWhiteToolbarActivity;
import com.aurora.source.center.activity.AddBatchActivity;
import com.aurora.source.entity.SysPermissionEntity;
import com.aurora.source.home.adapter.BatchManagementAdapter;
import com.aurora.source.home.bean.BatchManagementBean;
import com.aurora.source.home.presenter.BatchManagementPresenter;
import com.aurora.source.home.view.IBatchManagementView;
import com.aurora.source.util.DpPxUtil;
import com.aurora.source.util.GreenDaoHelper;
import com.aurora.source.util.SPUtils;
import com.aurora.source.widget.ItemDivider;
import com.aurora.source.widget.RxDialogSureCancel;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 主批次管理
 */
public class BatchManagementActivity extends BaseWhiteToolbarActivity<BatchManagementPresenter> implements IBatchManagementView {

    private static final int ADD_BATCH = 1001;
    private static final int CHANGE_BATCH_NAME = 1002;
    @BindView(R.id.iv_collect)
    ImageView ivCollect;
    @BindView(R.id.rv_recyclerView)
    RecyclerView rvRecyclerView;
    @BindView(R.id.iv_seek)
    ImageView ivSeek;
    @BindView(R.id.et_seek)
    EditText etSeek;

    private BatchManagementAdapter batchManagementAdapter;
    BatchManagementBean.BatchListBean changeBatchData;//需要修改的批次item
    private boolean addBatch;

    @Override
    public BatchManagementPresenter attach() {
        return new BatchManagementPresenter(this);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_batch_management;
    }

    @Override
    public void initData(@Nullable Bundle bundle) {
        SysPermissionEntity sysPermissionEntity2 = GreenDaoHelper.getDaoSession().getSysPermissionEntityDao().loadAll().get(0);
        addBatch = sysPermissionEntity2.getAddBatch();
    }

    private void refreshUI(){
        Map<String, String> params = new HashMap<>();
        params.put("userId", (String) SPUtils.get(activity, SPUtils.USER_ID, ""));
        params.put("number", "1");
        String batchType = "0";
        basePresenter.batchManagement(activity, params,batchType);
    }

    @Override
    public void initView(Bundle savedInstanceState, View contentView) {
        simpleToolbar.setMainTitle("基础批次管理");
        simpleToolbar.setLeftVisibility(View.VISIBLE);
        /*simpleToolbar.setRightTitleText("新增批次");
        simpleToolbar.setRightTitleClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoActivity(AddBatchActivity.class,false);
            }
        });*/
        batchManagementAdapter = new BatchManagementAdapter();
        batchManagementAdapter.setOnNameClick(new BatchManagementAdapter.OnNameClick() {//修改名称
            @Override
            public void onNameClick(final BatchManagementBean.BatchListBean data, int position) {
                if(!addBatch){
                    showToast("暂无权限");
                    return;
                }
                changeBatchData = data;
                Bundle bundle = new Bundle();
                bundle.putBoolean("isEdit",true);
                bundle.putString("batchName",changeBatchData.getBatchTitle());
                gotoActivityForResult(AddBatchActivity.class,CHANGE_BATCH_NAME,bundle);
            }
        });
        batchManagementAdapter.setOnStateClick(new BatchManagementAdapter.OnStateClick() {//修改状态
            @Override
            public void onStateClick(BatchManagementBean.BatchListBean data, int position) {
                if(!addBatch){
                    showToast("暂无权限");
                    return;
                }
                changeBatchState(data);

            }
        });
        rvRecyclerView.setAdapter(batchManagementAdapter);
        rvRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        rvRecyclerView.addItemDecoration(new ItemDivider().setDividerColor(Color.parseColor(getString(R.color.background_gray))).setDividerWith(DpPxUtil.dp2px(this, 5)));

    }

    //根据目前状态点击修改状态
    private void changeBatchState(final BatchManagementBean.BatchListBean data) {
        switch (data.getBatchStatus()){
            case "SUCCESS"://已启用，点击停用
                final RxDialogSureCancel rxDialogSureCancel = new RxDialogSureCancel(activity);//提示弹窗
                rxDialogSureCancel.setTitle("状态变更");
                rxDialogSureCancel.setContent("是否停用该批次");
                rxDialogSureCancel.setSureListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Map<String,String> params = new HashMap<>();
                        params.put("id",data.getId());
                        params.put("batchStatus","ERROR");
                        basePresenter.changeBatchNameOrState(params);
                        rxDialogSureCancel.cancel();
                    }
                });
                rxDialogSureCancel.setCancelListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        rxDialogSureCancel.cancel();
                    }
                });
                rxDialogSureCancel.show();

                break;
            case "ING"://待启用,点击启用
                final RxDialogSureCancel rxDialogSureCancel2 = new RxDialogSureCancel(activity);//提示弹窗
                rxDialogSureCancel2.setTitle("状态变更");
                rxDialogSureCancel2.setContent("是否启用该批次");
                rxDialogSureCancel2.setSureListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Map<String,String> params = new HashMap<>();
                        params.put("id",data.getId());
                        params.put("batchStatus","SUCCESS");
                        basePresenter.changeBatchNameOrState(params);
                        rxDialogSureCancel2.cancel();
                    }
                });
                rxDialogSureCancel2.setCancelListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        rxDialogSureCancel2.cancel();
                    }
                });
                rxDialogSureCancel2.show();
                break;
            case "ERROR"://停用,点击启用
                final RxDialogSureCancel rxDialogSureCancel3 = new RxDialogSureCancel(activity);//提示弹窗
                rxDialogSureCancel3.setTitle("状态变更");
                rxDialogSureCancel3.setContent("是否再次启用该批次");
                rxDialogSureCancel3.setSureListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Map<String,String> params = new HashMap<>();
                        params.put("id",data.getId());
                        params.put("batchStatus","SUCCESS");
                        basePresenter.changeBatchNameOrState(params);
                        rxDialogSureCancel3.cancel();
                    }
                });
                rxDialogSureCancel3.setCancelListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        rxDialogSureCancel3.cancel();
                    }
                });
                rxDialogSureCancel3.show();
                break;
        }
    }

    @Override
    public void doBusiness() {
        refreshUI();
    }



    @OnClick({R.id.iv_collect, R.id.iv_seek})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_collect:
                if(!addBatch){
                    showToast("暂无权限");
                    return;
                }
                gotoActivityForResult(AddBatchActivity.class, ADD_BATCH);
                break;
            case R.id.iv_seek:
                Map<String, String> params = new HashMap<>();
                params.put("userId", (String) SPUtils.get(activity, SPUtils.USER_ID, ""));
                params.put("number", "1");
                params.put("batchTitle",etSeek.getText().toString());
                basePresenter.batchManagement(activity, params,"0");
                break;

        }
    }

    @Override
    public void onBatchManagementSuccess(BatchManagementBean data) {
        batchManagementAdapter.setDataList(data.getBatchList());
    }

    @Override
    public void onBatchManagementFail(String msg) {
        showToast(msg);
    }

    @Override
    public void onChangeSuccess(String msg) {
        showToast(msg);
        refreshUI();
    }

    @Override
    public void onChangeFail(String msg) {
        showToast(msg);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case ADD_BATCH:
                refreshUI();
                break;
            case CHANGE_BATCH_NAME:
                if(resultCode==RESULT_OK){
                    String content = data.getStringExtra("batchName");
                    Map<String,String> params = new HashMap<>();
                    params.put("id",changeBatchData.getId());
                    params.put("batchTitle",content);
                    basePresenter.changeBatchNameOrState(params);
                }
                break;
        }
    }
}
